package com.zhijian.warehouse.manage.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.warehouse.manage.dao.MessageMapper;
import com.zhijian.warehouse.manage.entity.po.Message;
import com.zhijian.warehouse.manage.entity.vo.request.MessageSearchVo;
import com.zhijian.warehouse.manage.entity.vo.response.MessageVo;
import com.zhijian.warehouse.manage.enums.MessageEnum;
import com.zhijian.warehouse.manage.service.MessageService;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 消息通知业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-06-07 15:41:53
 */
@Slf4j
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper mapper;

    @Override
    public Message selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public MessageVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(Message message) {
        Date now = new Date();
        message.setCreateTime(now);
        message.setUpdateTime(now);
        return mapper.insert(message) > 0 ? message.getId() : 0L;
    }

    @Override
    public boolean update(Message message) {
        Date now = new Date();
        message.setUpdateTime(now);
        return mapper.updateByPrimaryKey(message) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(Message record) {
        record.setUpdateTime(new Date());
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(MessageSearchVo messageSearchVo) {
        return mapper.count(messageSearchVo);
    }

    @Override
    public List<MessageVo> selectList(MessageSearchVo messageSearchVo) {
        return mapper.selectList(messageSearchVo);
    }

    @Override
    public Page<MessageVo> selectPage(MessageSearchVo messageSearchVo) {
        //先直接查总数
        int count = count(messageSearchVo);
        //分页
        Page<MessageVo> page = new Page<>(true, count, messageSearchVo.getPageNum(), messageSearchVo.getPageSize());
        messageSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<MessageVo> responseVos = this.selectList(messageSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean read(Long id) {
        return this.updateByPrimaryKeySelective(Message.builder()
                .id(id)
                .weatherRead(MessageEnum.MessageWeatherReadEnum.ALREADY_READ.getCode().byteValue())
                .build());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean read(List<Long> idList) {
        idList.forEach(this::read);
        return true;
    }

    @Override
    public boolean insertBatch(List<Message> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

}